package com.fhz.high5kotlin

/**
 * 时间:2023/3/21
 * @author Mr.Feng
 * 简述: TODO
 * 方法名,参数列表,返回值,方法体
 */

val P = 3.141592653

internal fun te(){
    println(1)
}

fun main(){


    "test".length
    "test".len()


    val isEven = IntPredicate { it % 2 == 0 }

te()
    val s = Student()
    s.name = "aadfsdf" //set
    println(s.name) //get

    Student.ADDRESS //tianjin


//    var a  = 1
//    var s1 = "a is $a"
//    val s2 = "${s1.replace("is", "was")}, but now is $a"
//    println(s2)
//    maxOf(1,1)

    val oneMillion = 1_000_000


    val listOf = listOf("apple", "huawei", "xiaomi")
    when {
        "orange" in listOf -> println("juicy")
        "apple" in listOf -> println("apple is fine too")
    }

    val fruits = listOf("aaa", "aab", "aac", "bbb")
    fruits
        .filter { it.startsWith("a") }
        .sortedBy { it }
        .map { it.toUpperCase() }
        .forEach { println(it) }


//    if(a > b){
//        a
//    }
    var x = 1

    when (x) {
        1 -> {
            print("x == 1")
            print("x == 1")
            print("x == 1")
            print("x == 1")
            print("x == 1")
            print("x == 1")
        }
        2 -> print("x == 2")
        else -> { // 注意这个块
            print("x is neither 1 nor 2")
        }
    }

//    for (i in listOf.indices){
////        println(i)
//        println("item at $i is ${listOf[i]}")
//    }

//    var index = 0
//    while (index < listOf.size)	 {
//        println("item at $index is ${listOf[index]}")
//        index++
//    }
    val describe = describe("123")
    println(describe)
}


fun describe(obj: Any): String =
    when (obj) {
        1          -> "One"
        "Hello"    -> "Greeting"
        is Long    -> "Long"
        !is String -> "Not a string"
        else       -> "Unknown"
    }

fun parseInt(str: String): Int? {
    return null
}

fun printProduct(arg1: String, arg2: String) {
    val x = parseInt(arg1)
    val y = parseInt(arg2)

    // 直接使用 `x * y` 会导致编译错误，因为它们可能为 null
    if (x != null && y != null) {
        // 在空检测后，x 与 y 会自动转换为非空值（non-nullable）
        println(x * y)
    }
    else {
        println("'$arg1' or '$arg2' is not a number")
    }
}

fun maxOf(a: Int, b: Int) = if (a > b)  a else b

fun getStringLength(obj: Any): Int? {
    if (obj is String) {
        // `obj` 在该条件分支内自动转换成 `String`
        return obj.length
    }else if(obj is Int){
        return obj
    }else if(obj is Number){

    }

    // 在离开类型检测分支后，`obj` 仍然是 `Any` 类型
    return null
}
fun getStringLength1(obj: Any): Int? {
    // `obj` 在 `&&` 右边自动转换成 `String` 类型
    if (obj is String && obj.length > 0) {
        return obj.length
    }
    return null
}
/**
 *
 */
//
fun sum(a:Int,b:Int) : Int{
    println(P)
    return a + b
}
fun sum1(a:Int,b:Int) = a + b